package com.sun.net;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Handler;
import android.text.format.DateFormat;

import com.tools.HttpInterface;
import com.tools.SysInterfacePhone;

public class BaseDao {
	public static final String	KEY			= "helloworld";						// md5Key值 测试key值
	private static final String	INTERFACE	= "http://mobile.cpdyj.com/service";
	Handler						handler;
	
	public BaseDao(Handler handler) {
		this.handler = handler;
	}
	
	public interface HttpListener {
		
		public void onEvent(List<String> list);
	}
	
	// 获得当前时间
	public static String GetCurrentTime() {
		// 2012-03-10 00:58:24
		return DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
	}
	
	// 设置请求参数
	public static String initParameter(String requestContent) {
		String string = "";
		try {
			string = "xml=" + Base64.encode(requestContent.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return string;
	}
	
	public void Redo(final String requestContent, final HttpListener listener) {
		new Thread() {
			public void run() {
				String value = new HttpInterface().doNetwork(INTERFACE, requestContent);
				final List<String> list = resolveXML(value);
				handler.post(new Runnable() {					
					@Override
					public void run() {
						listener.onEvent(list);						
					}
				});
			}
		}.start();
		
	}
	
	public String GetMD5(String time) {
		return SysInterfacePhone.invoker().toMD5((time + KEY).getBytes());
	}
	
	protected static ArrayList<String> resolveXML(String xml) {
		ArrayList<String> list = new ArrayList<String>();
		InputStreamReader reader = null;
		try {
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();
			ByteArrayInputStream bytes = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			reader = new InputStreamReader(bytes, "UTF-8");
			parser.setInput(reader);
			int parserEvent = parser.getEventType();
			boolean done = false;
			while (parserEvent != XmlPullParser.END_DOCUMENT && !done) {
				
				// 访问文档根节点
				if (parserEvent == XmlPullParser.START_TAG) {
					int attrCount = parser.getAttributeCount();
					for (int i = 0; i < attrCount; i++) {
						list.add(parser.getAttributeValue(i));
					}
					// 这里可以根据不同的 节点名称来作出不同的反应，比如text需要单独处理什么
					
				} else if (parserEvent == XmlPullParser.TEXT) {
					if (parser.getText().trim().length() != 0) {
						list.add(parser.getText());
					}
					
				} else if (parserEvent == XmlPullParser.END_TAG) {
					String rootName = parser.getName();
					int num1 = xml.indexOf("</" + rootName + ">");
					int num2 = xml.indexOf("<" + rootName + ">");
					xml = xml.substring(num1 + rootName.length() + 3);
					int num3 = num1 - num2;
					int num4 = rootName.length() + 2;
					if (num3 == num4) {
						list.add("");
					}
					
				}
				parserEvent = parser.next();
			}
		} catch (Exception e) {
			list.add("-0001");
			list.add("解读数据错误!");
			// list.add("解读数据错误");
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
				
			} catch (Exception ioe) {
				ioe.printStackTrace();
			}
		}
		return list;
	}
}
